package br.com.yurikimo.ykkit;

import java.util.Vector;

import android.widget.RelativeLayout;
import android.widget.ScrollView;

public class YKScrollView extends ScrollView implements YKContainer, YKBaseComponent{	
	private RelativeLayout.LayoutParams layoutParams;
	private YKContainer parent = null;
	private float x = 0;
	private float y = 0;
	private float width = 100;
	private float height = 50;
	private RelativeLayout relativeLayout = null;
	private RelativeLayout contentLayout = null;
	private float lastTop = 0;
	
	private Vector<Object> childreanList = new Vector<Object>();
	
	public YKScrollView(){
		super(YKDirector.getActivity());
		
		contentLayout = new RelativeLayout(YKDirector.getActivity());
		contentLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
		
		addView(contentLayout);
	}
	
	public YKScrollView(float x, float y, float width, float height){
		super(YKDirector.getActivity());
		
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
		
		setBounds(x, y, width, height);
		
		contentLayout = new RelativeLayout(YKDirector.getActivity());
		contentLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
		
		addView(contentLayout);
	}
		
	public void removeAllChildrean(){
		for (int i = 0; i < childreanList.size(); i++){
				((YKBaseComponent)childreanList.elementAt(i)).removeFromSuperView();
		}

		childreanList.removeAllElements();
		
		lastTop = 0;
	}

	public int getChildreanCount(){
		return childreanList.size();
	}
	
	public Object getChidAtIndex(int index){
		return childreanList.elementAt(index);
	}
	
	public void addChild(YKBaseComponent child){
		childreanList.add(child);
		child.setObjectParent(this);
		
		child.setPosition(child.getObjectX(), child.getObjectY());
		
		child.addToLayout(contentLayout);
	}
	
	public void addChildAsList(YKBaseComponent child){
		childreanList.add(child);
		child.setObjectParent(this);
		
		child.setPosition(child.getObjectX(), lastTop);
		
		lastTop = child.getObjectY() + child.getObjectHeight();
		
		child.addToLayout(contentLayout);
	}
	
	public void setBounds(float x, float y, float width, float height){
		layoutParams = null;
		
		layoutParams = new RelativeLayout.LayoutParams((int)width, (int)height);
		
		layoutParams.leftMargin = (int)x;
		layoutParams.topMargin = (int)y;				
	}
	
	public void addToLayout(RelativeLayout relativeLayout){
		this.relativeLayout = relativeLayout;
		
		relativeLayout.addView(this, layoutParams);
		relativeLayout.invalidate();
	}
	
	public float getObjectWidth(){
		return width;
	}
	
	public float getObjectHeight(){
		return height;
	}
	
	public void refreshLayout(){
		setBounds(x, y, width, height);
		
		if (relativeLayout != null){
			if (relativeLayout.indexOfChild(this) >= 0){
				relativeLayout.removeView(this);
				
				relativeLayout.addView(this, layoutParams);
				relativeLayout.invalidate();
			}
		}
	}
	
	public void setPosition(float x, float y){
		this.x = x;
		this.y = y;
	
		refreshLayout();
	}

	public void uncheckButtons(YKButton button){
		for (int i = 0; i < childreanList.size(); i++){
			if (childreanList.get(i) instanceof YKButton){
				YKButton btnTemp = (YKButton)childreanList.get(i);
				
				if (btnTemp.isCheckButton() && !btnTemp.equals(button)){
					btnTemp.chek(false);
				}
			}
		}
	}

	public RelativeLayout getLayoutView()
	{
		if (relativeLayout == null){
			relativeLayout = new RelativeLayout(getContext());
			relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
		}
		
		return relativeLayout;
	}

	public float getObjectX()
	{
		return x;
	}

	public float getObjectY()
	{
		return y;
	}

	public void removeFromSuperView()
	{
		relativeLayout.removeView(this);
	}

	public YKContainer getObjectParent()
	{
		return parent;
	}

	public void setObjectParent(YKContainer parent)
	{
		this.parent = parent;
	}
}
